import utils from "../common/util.js"

export default {

	// 根据策略获取基金菜单
	findListByModel(model){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/list/'+model,
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 根据基金ID获取最新净值
	getNewestNumber(code){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/newest/'+code,
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 根据策略获取某只基金列表数据
	findByCodeAndModel(code,model){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/'+code+"/"+model,
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 买入基金
	buy(jijin){
		// 修改buyDay格式  LocalDateTime格式
		jijin.buyDay = jijin.buyDay+"T00:00:00"
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store',
				method: 'POST',
				data:{
					...jijin
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve('ok')
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 查询基金名称
	getNameByCode(code){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/name/'+code,
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 卖出基金
	sell(sellId){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/sell/'+sellId,
				method: 'PUT',
				data:{
					isSell: 1,
					isOk: 0
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve("ok")
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 撤回买入
	callbackBuy(sellId){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/'+sellId,
				method: 'DELETE',
				data:{},
				success: function(res) {
					if(res.data.code === 20000){
						resolve("ok")
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 撤回卖出
	callbackSell(sellId){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/recall/'+sellId,
				method: 'PUT',
				data:{},
				success: function(res) {
					if(res.data.code === 20000){
						resolve("ok")
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 查询所有交易中的基金
	getIsDoing(){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/doing',
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	
	// 分页查询所有交易完成的基金
	getIsDone(page,size){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/finish/'+page+"/"+size,
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	},
	
	// 查询某条交易订单详情
	getOrderDetail(orderId){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl+'/store/'+orderId,
				method: 'GET',
				data:{
				},
				success: function(res) {
					if(res.data.code === 20000){
						resolve(res.data.data)
					}else{
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: '网络错误',
						showCancel: false
					});
				}
			})
		})
	}
	
}